import { getProdUnionResultApi, prodClickCountApi } from 'src/api/prod';

export type ProdItem = {
  applyNumber: string;
  descInfo: string;
  detailUrl: string;
  itRate: string;
  lnAmount: string;
  lnPeriod: string;
  logoUrl: string;
  productId: string;
  productLabel: string;
  productName: string;
  typeStr: string;
  unionFlag: number;
};

export type ResType = {
  dataList: ProdItem[];
};

type PushOrderInfo = {
  createTime: string;
  dateInfo: string;
  id: number;
  infoId: number;
  infoName: string;
  infoType: string;
  originAmount: number;
  priceAmount: number;
  pushId: string;
  status: string;
  taskId: number;
  uid: number;
  updateTime: string;
};

export type CheckUserRes = {
  companyLogo: string;
  companyName: string;
  infoId: number;
  infoName: string;
  infoType: string;
  itemInfoId: number;
  maxLoanAmount: string;
  maxRate: string;
  minRate: string;
  priceAmount: number;
  productName: string;
  protocolUrl: string;
  pushOrderNo: string;
  pushSuccess: boolean;
  settleFlag: boolean;
  uid: number;
  pushOrderInfo: PushOrderInfo;
};

export const gotoProd = async (dataExt: number, prod?: ProdItem) => {
  if (!prod) return;
  await prodClickCountApi({ dataId: prod.productId, operType: '2', dataExt: dataExt });

  if (prod.unionFlag) {
    if (!prod.detailUrl) {
      const res = await getProdUnionResultApi({ productId: prod.productId });
      window.location.href = res.data.productUrl;
      return;
    }
  }
  if (prod.detailUrl) {
    window.location.href = prod.detailUrl;
  }
};
